package com.kgc.manger.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.dao.TbItemMapper;
import com.kgc.entity.TbItem;
import com.kgc.entity.TbItemExample;
import com.kgc.manger.service.ItemService;
import com.kgc.manger.utils.IDUtils;
import com.kgc.utils.ResultTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private TbItemMapper tbItemMapper;
    @Override
    public ResultTable findAll(int page, int limit, Long cid) {
        PageHelper.startPage(page,limit);
        TbItemExample example=new TbItemExample();
        if(cid!=null&&cid>0){
            TbItemExample.Criteria criteria = example.createCriteria();
            criteria.andCidEqualTo(cid);
        }
        List<TbItem> list=tbItemMapper.selectByExample(example);
        PageInfo pageInfo=new PageInfo(list);
        ResultTable resultTable=new ResultTable();
        resultTable.setCode(0);
        resultTable.setMsg("ok");
        resultTable.setCount(pageInfo.getTotal());
        resultTable.setData(pageInfo.getList());
        return  resultTable;
    }


    @Override
    public void addItem(TbItem item) {
        item.setId(IDUtils.genItemId());
        item.setStatus((byte)1);
        item.setUpdated(new Date());
        item.setCreated(new Date());
        tbItemMapper.insert(item);
    }
}
